<template>
  <div class="shopcar-container">
    <div class="goods-list">

        <!-- 商品列表区域 -->
        <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <mt-switch 
                        v-model="$store.getters.getGoodSelected[item.id]"
                        @change=selectedChanged(item.id,$store.getters.getGoodSelected[item.id])>
                    </mt-switch>
                    <img :src="item.thumb_path">

                    <div class="info">
                        <h1>{{item.title}}</h1>
                        <p>
                            <span class="price">￥{{item.sell_price}}</span>
                            <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                            <!-- 
                                问题：如何从购物车中获取商品的数量？
                                1、创建一个空对象，然后循环购物车中所有的商品数据，把商品ID作为key，count的值作为value即可，如{88:5,89:5}
                                {{$store.getters.getGoodsCount[item.id]}}
                             -->
                            <a href="#" @click.prevent="remove(item.id,i)">删除</a>
                        </p>
                    </div>
                </div>
            </div>

        </div>

        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计(不含运费)</p>
                        <p>已勾选商品 
                            <span class="num-show">
                            {{$store.getters.getGoodsCountAndAmount.count}}    
                            </span> 件，总价 ￥
                            <span class="num-show">{{$store.getters.getGoodsCountAndAmount.amount}}</span>
                        </p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
    </div>
     <p>{{$store.getters.getGoodSelected}}</p>
</div>  

</template>

<script>
import numbox from '../subcomponents/shopcar_numbox.vue'
export default {
    data(){
        return{
            goodslist:[] //购物车中所有商品的数据
        }
    },
    created(){
        this.getGoodsList();
    },
    methods:{
        //获取购物车页面数据 通过this.$store.state.car里面的id获取
        getGoodsList(){
             /* 
                    1、获取到store中所有的商品的ID，然后拼接一个用逗号分隔的字符串
                */
            var idArr = [];
            this.$store.state.car.forEach(item => {
                idArr.push(item.id);
            });
            if(idArr.length <= 0){
                return;
            }
            //idArr.join(",")把数组转换成字符串
            this.$http.get('goods/getshopcarlist/'+idArr.join(",")).then(res => {
                if(res.body.status === 0){
                    this.goodslist = res.body.message;
                }
            })
        },
        remove(id,index){
            //点击删除，把商品从store中根据id删除，根据index删除当前组件中的goodslist
            //删除组建中的goodlist
            this.goodslist.splice(index,1);

            this.$store.commit('removeFromCar',id);
        },
        selectedChanged(id,value){
            //点击开关，将最新的开关状态同步到store中
            console.log(id+" = "+value);
            this.$store.commit('updateGoodsSlected',{id:id,selected:value});
        }
    },
    components:{
        numbox
    }
};
</script>

<style lang="scss" scoped>
.shopcar-container{
    background-color: #eee;
    overflow: hidden;
    .goods-list{
        .mui-card-content-inner{
            display: flex;
            align-items: center;
        }
        img{
            width: 50px;
            height: 50px;
        }
        h1{
            font-size: 13px;
        }
        .info{
            display:flex;
            flex-direction: column;
            justify-content: space-between;

            .price{
                color:red;
                font-weight: bold;
            }
        }
    }
    .jiesuan{
        display: flex;
        justify-content: space-between;
        align-items: center;

        .num-show{
            color: red;
            font-weight: bold;
            font-size: 16px;
        }
    }
}
</style>